package com.lenovo.lcdm.team.controller;

import com.lenovo.lcdm.common.enums.ResponseMsgEnum;
import com.lenovo.lcdm.common.model.ResponseVo;
import com.lenovo.lcdm.team.common.util.ContextUtil;
import com.lenovo.lcdm.team.dto.GroupDto;
import com.lenovo.lcdm.team.dto.GroupTreeDto;
import com.lenovo.lcdm.team.entity.TeamGroup;
import com.lenovo.lcdm.team.entity.TeamUser;
import com.lenovo.lcdm.team.entity.TeamUserGroup;
import com.lenovo.lcdm.team.service.ITeamGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/team")
public class GroupController {

    @Autowired
    private ITeamGroupService groupService;
    @GetMapping("/group/root")
    public ResponseVo<List<GroupTreeDto>> getRoot() {
        ResponseVo<List<GroupTreeDto>> responseVo = new ResponseVo<>();
        List<GroupTreeDto> data = groupService.getRoot();
        responseVo.setData(data);
        return responseVo;
    }

    @GetMapping("/group/{id}")
    public ResponseVo<GroupTreeDto> getGroupInfo(@PathVariable("id") Long id, @RequestParam(required = false) String onlyRole) {
        ResponseVo<GroupTreeDto> responseVo = new ResponseVo<>();
        try {
            GroupTreeDto data = groupService.getGroupInfo(id, Boolean.getBoolean(onlyRole));
            responseVo.setData(data);
        }catch (Exception e){
            responseVo.setCode(ResponseMsgEnum.FAIL.getCode());
            responseVo.setMessage(e.getMessage());
        }
        return responseVo;
    }

    @GetMapping("/group/all")
    public ResponseVo<List<TeamGroup>> getAllGroup() {
        ResponseVo<List<TeamGroup>> responseVo = new ResponseVo<>();
        List<TeamGroup> data = groupService.list();
        responseVo.setData(data);
        return responseVo;
    }

    @GetMapping("/group/{id}/users")
    public ResponseVo<List<TeamUser>> getGroupUsers(@PathVariable("id") Long id) {
        ResponseVo<List<TeamUser>> responseVo = new ResponseVo<>();
        List<TeamUser> data = groupService.getGroupUsers(id);
        responseVo.setData(data);
        return responseVo;
    }

    @PostMapping("/group")
    public ResponseVo saveGroup(@RequestBody GroupDto groupDto) {
        ResponseVo responseVo = new ResponseVo<>();
        try {
            if(groupDto.getId() == null){
                groupDto.setCreator(ContextUtil.getCurrentUser());
            }
            groupDto.setModifier(ContextUtil.getCurrentUser());
            groupService.saveGroup(groupDto);
            responseVo.setMessage(ResponseMsgEnum.SUCCESS.getMessage());
        }catch (Exception e){
            responseVo.setCode(ResponseMsgEnum.FAIL.getCode());
            responseVo.setMessage(e.getMessage());
        }
        return responseVo;
    }

    @DeleteMapping("/group/{id}")
    public ResponseVo deleteGroup(@PathVariable("id") Long id) {
        ResponseVo responseVo = new ResponseVo<>();
        try {
            groupService.deleteGroup(id);
        }catch (Exception e){
            responseVo.setCode(ResponseMsgEnum.FAIL.getCode());
            responseVo.setMessage(e.getMessage());
        }
        return responseVo;
    }

    @PutMapping("/group/grant/{groupId}")
    public ResponseVo addUserGroup(@PathVariable("groupId") Long groupId, @RequestBody TeamUserGroup userGroup) {
        ResponseVo responseVo = new ResponseVo<>();
        try {
            userGroup.setGroupId(groupId);
            userGroup.setCreator(ContextUtil.getCurrentUser());
            userGroup.setModifier(ContextUtil.getCurrentUser());
            groupService.addUserGroup(userGroup);
        }catch (Exception e){
            responseVo.setCode(ResponseMsgEnum.FAIL.getCode());
            responseVo.setMessage(e.getMessage());
        }
        return responseVo;
    }

    @PutMapping("/group/revoke/{groupId}")
    public ResponseVo deleteUserGroup(@PathVariable("groupId") Long groupId, @RequestBody GroupDto groupDto) {
        ResponseVo responseVo = new ResponseVo<>();
        try {
            groupService.deleteUserGroup(groupId, groupDto.getUserIds());
        }catch (Exception e){
            responseVo.setCode(ResponseMsgEnum.FAIL.getCode());
            responseVo.setMessage(e.getMessage());
        }
        return responseVo;
    }

    @PutMapping("/group/{groupId}/role/{roleId}")
    public ResponseVo deleteGroupRole(@PathVariable("groupId") Long groupId, @PathVariable Long roleId) {
        ResponseVo responseVo = new ResponseVo<>();
        try {
            groupService.deleteGroupRole(groupId, roleId);
        }catch (Exception e){
            responseVo.setCode(ResponseMsgEnum.FAIL.getCode());
            responseVo.setMessage(e.getMessage());
        }
        return responseVo;
    }

}
